<template>
  <div class="container">
    <el-button type="primary" size="small" class="add-button" @click="addDrawer()">添加分类</el-button>
    <!-- 列表 -->
    <el-table :data="typeList" border style="width: 100%">
      <el-table-column prop="typeId" label="编号" />
      <el-table-column prop="typeName" label="分类" />
      <el-table-column prop="typeBlogCount" label="博客数" />

      <el-table-column prop="enable" label="启用">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enable === 1">启用</el-tag>
          <el-tag v-else type="info">未启用</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row.typeId)">编辑</el-button>
          <el-button v-if="scope.row.enable === 0" size="mini" type="success" @click="enableById(scope.row.typeId)">启用</el-button>
          <el-button v-else size="mini" type="warning" @click="disEnableById(scope.row.typeId)">弃用</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.typeId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加弹框 -->
    <el-drawer
      ref="drawer"
      title="添加分类"
      :visible.sync="drawer"
    >
      <!-- 向另一个页面传递 方法 -->
      <TypeAdd @getTypeList="getTypeList" @closeAddDrawer="closeAddDrawer" />
    </el-drawer>
    <!-- 修改弹窗 -->
    <el-dialog
      title="修改分类"
      :visible.sync="updateDialog"
    >
      <!-- 向另一个页面传递 方法 -->
      <typeUpdate :type="type" @closeUpdateDialog="closeUpdateDialog" @getTypeList="getTypeList" />
    </el-dialog>
  </div>
</template>
<script>
// 引入接口
import typeApi from '@/api/type'
import TypeAdd from './type-add'
import typeUpdate from './type-update'
export default {
  components: {
    TypeAdd,
    typeUpdate
  },
  data() {
    return {
      type: {}, // 分类
      updateDialog: false, // 控制修改弹窗
      drawer: false, // 控制添加抽屉打开关闭
      typeList: [] // 类型数组
    }
  },
  created() {
    this.getTypeList()
  },
  methods: {
    getTypeList() {
      // 查询分类列表
      typeApi.findAll().then(response => {
        console.log(response)
        this.typeList = response.data
      })
    },
    handleEdit(id) {
      // 修改分类方法
      typeApi.findById(id).then(response => {
        this.type = response.data
        // 获取到数据
        this.updateDialog = true
      })
    },
    handleDelete(id) {
      // 删除分类方法
      this.$confirm('此操作将永久删除！, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      })
        .then(() => {
          typeApi.deleteById(id).then(response => {
            this.$message.success(response.msg)
            // 删除成功 刷新列表
            this.getTypeList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addDrawer() {
      // 打开弹窗
      this.drawer = true
    },
    closeAddDrawer() {
      // 关闭添加分类窗口
      this.drawer = false
    },
    closeUpdateDialog() {
      // 关闭修改弹窗
      this.updateDialog = false
    },
    enableById(id) {
      // 启用 分类
      typeApi.enableById(id).then(response => {
        this.$message.success(response.msg)
        // 启用成功 刷新列表
        this.getTypeList()
      })
    },
    disEnableById(id) {
      // 弃用分类
      typeApi.disEnableById(id).then(response => {
        this.$message.success(response.msg)
        // 弃用成功 刷新列表
        this.getTypeList()
      })
    }
  }
}
</script>
